(function() {
    'use strict';
    angular.module('weApp.controllers')
            //评价
            .controller('assessCtrl', ['$ionicActionSheet', '$ionicPopup', '$scope', '$rootScope', '$state', '$stateParams', '$timeout', '$http', 'weAppDataBase', 'showLinePageService', 'localStorageService', 'myFavorsService',
                function($ionicActionSheet, $ionicPopup, $scope, $rootScope, $state, $stateParams, $timeout, $http, weAppDataBase, showLinePageService, localStorageService, myFavorsService) {

                    if ($rootScope.lineData) {
                        localStorageService.set("lineData", JSON.stringify($rootScope.lineData));
                    } else {
                        $rootScope.lineData = JSON.parse(localStorageService.get("lineData"));
                    }

                    if ($rootScope.busNums) {
                        localStorageService.set("busNums", JSON.stringify($rootScope.busNums));
                    } else {
                        $rootScope.busNums = JSON.parse(localStorageService.get("busNums"));
                    }

                    //车辆投诉上拉弹窗
                    $scope.show = function() {
                        var obj = {};
                        obj.buttons = [];
                        obj.titleText = '请选择投诉车辆(默认为线路投诉)';
                        obj.cancelText = '取消';
                        obj.cancel = function() {
                        };
                        obj.buttonClicked = function(i) {
                            if (i > 0) {
                                $scope.busNum = $rootScope.busNums[i - 1].busNum;
                                $('.select').html($scope.busNum);
                            } else {
                                $('.select').html('请选择投诉车辆(默认为线路投诉)');
                                $scope.busNum = '';
                            }
                            return true;
                        };
                        obj.buttons.push({text: '<span class="ass-t">请选择投诉车辆(默认为线路投诉)</span>'});
                        for (var i in $rootScope.busNums) {
                            if (i >= 0)
                                obj.buttons.push({text: $rootScope.busNums[i].busNum});
                        }
                        var hideSheet = $ionicActionSheet.show(obj);

                    }

                    //获取投诉列表
                    var feedReq = {
                        method: 'post',
                        url: $rootScope.rootLocation + '/line/bus/feedbackList',
                        data: {
                            "cityName": $rootScope.cityName,
                            "cityCode": $rootScope.cityCode,
                            "problemType": '1'
                        }
                    };

                    $http(feedReq).then(function(feedInfos) {
                        if (feedInfos.data.head.success == "true") {
                            $scope.feedInfo = feedInfos.data.data.problems;
                        }

                    });


                    //投诉复选
                    var content = {};
                    var problem = {}
                    var a = document.getElementById("group");
                    var list = a.querySelectorAll("button");
                    $rootScope.feedBox = function(e, i, text, prob) {
                        var t = $(e.currentTarget);
                        t.toggleClass('active');
                        if (t.is('.active')) {
                            content[text] = 0;
                            problem[prob] = 0;
                        } else {
                            delete content[text];
                            delete problem[prob];
                        }
                    }


                    //车辆投诉提交
                    $scope.feedCar = function() {

                        var desc = $("#desc").val();
                        if (desc) {
                            content[desc] = 0;
                        }
                        //表单验证
                        if ($.isEmptyObject(content)) {
                            var myPopup = $ionicPopup.show({
                                title: "友情提示",
                                template: "请填写内容后再试",
                                cssClass: "validate"
                            });

                        }
                        if (myPopup) {
                            $timeout(function() {
                                myPopup.close();
                            }, 2000);
                            return false;
                        }
                        $scope.content = Object.keys(content).join(',');
                        $scope.problemId = Object.keys(problem).join();
                        var rootCar = {
                            method: 'post',
                            url: $rootScope.rootLocation + '/line/bus/complaint',
                            data: {
                                "selfNum": " ",
                                "busNum": $scope.busNum,
                                "lineName": $rootScope.lineData.lineName,
                                "lineId": $rootScope.lineData.lineId,
                                "content": $scope.content,
                                "problemId": $scope.problemId
                            }
                        };

                        $http(rootCar).then(function(data) {
                            var myPopu = $ionicPopup.show({
                                title: "温馨提示",
                                template: "评价成功!",
                                cssClass: "validate"
                            });

                            $timeout(function() {
                                myPopu.close();
                            }, 2000);
                            window.history.back();

                        });


                    }


                }]);
})();